package com.editso.haier.result.car;

import com.editso.haier.bean.CommodityBean;
import com.editso.haier.entity.Commodity;
import com.editso.haier.entity.ShoppingCar;
import com.editso.haier.result.DetailsAssets;
import com.editso.haier.util.Utils;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
public class DetailsShoppingCar {
    private String code;
    private String commodityName;
    private String commodityId;
    private BigDecimal price;
    private Integer count;
    private Date date;
    private List<DetailsAssets> assets;

    public static DetailsShoppingCar from(ShoppingCar shoppingCar){
        Commodity commodity = shoppingCar.getCommodity();
        return new DetailsShoppingCar(
                shoppingCar.getOrderCode(),
                commodity.getName(),
                commodity.getId(),
                commodity.getPrice(),
                shoppingCar.getCount(),
                shoppingCar.getCreateDate(),
                DetailsAssets.mergeAssets(commodity)
                        .getOrDefault(CommodityBean.AssetsOther.APPEARANCE.ordinal(),
                                new ArrayList<>())
        );
    }

    public static List<DetailsShoppingCar> from(Iterable<ShoppingCar> shoppingCars){
        return Utils.asList(shoppingCars)
                .stream()
                .map(DetailsShoppingCar::from)
                .collect(Collectors.toList());
    }

}
